package com.zhuzhe.blog.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.category.entity.Category;
import com.zhuzhe.blog.category.mapper.CategoryMapper;
import com.zhuzhe.blog.redisFinal.CategoryRedisFinal;
import com.zhuzhe.blog.category.requestDto.CategorySerachDto;
import com.zhuzhe.blog.category.responseVo.CategoryLabelListVo;
import com.zhuzhe.blog.category.responseVo.CategoryListVo;
import com.zhuzhe.blog.category.responseVo.CategoryVo;
import com.zhuzhe.blog.category.service.ICategoryService;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.utils.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 文章分类表服务实现 类
 */
@Service // 一定不要少了
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements ICategoryService {

    //装配redis
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ResponseData<IPage<CategoryVo>> queryPage(CategorySerachDto request) {
        // 查询条件对象
        QueryWrapper<Category> wrapper = new QueryWrapper();
        // 分类名称、状态
        wrapper.like(StringUtils.isNotEmpty(request.getName()),"name",request.getName())
                .eq(request.getStatus() != null,"status", request.getStatus())
                .orderByDesc("status").orderByAsc("sort");
        // 第1个参数是Page分页对象，第2个参数查询条件
        IPage<Category> data = baseMapper.selectPage(request.getPage(), wrapper);
        IPage<CategoryVo> categoryVoIPage = new Page<>();
        Convert.convertType(data,categoryVoIPage);
        List<CategoryVo> categoryVos = Convert.convertList(data.getRecords(),CategoryVo.class);
        categoryVoIPage.setRecords(categoryVos);
        return JsonData.success(categoryVoIPage);
    }

    @Override
    public ResponseData<List<CategoryListVo>> findAllNormal( ) {

        List<Object> objectList = redisUtil.lGet(CategoryRedisFinal.FIND_ALL_NORMAL_KEY,0,-1);
        List<CategoryListVo> categoryListVos = null;
        categoryListVos  =  Convert.convertList(objectList,CategoryListVo.class);
        if (null == categoryListVos || categoryListVos.size() == 0){
            QueryWrapper<Category> queryWrapper = new QueryWrapper();
            queryWrapper.eq(Category.STATUS,1)
                    .orderByAsc(Category.SORT); //正常状态为1 禁用为0
            categoryListVos  =  Convert.convertList(baseMapper.selectList(queryWrapper),CategoryListVo.class);
            //如果list 为nul没有必要存入redis
            if (null != categoryListVos && categoryListVos.size() != 0){
                redisUtil.lSet(CategoryRedisFinal.FIND_ALL_NORMAL_KEY,categoryListVos);
            }
        }
        return JsonData.success(categoryListVos);
    }

    @Override
    public ResponseData<List<CategoryLabelListVo>> findCategoryAndLabel() {
        String key = CategoryRedisFinal.FIND_CATEGORY_AND_LABE_LKEY;
        List<Object> objects = redisUtil.lGet(key,0,-1);
        List<CategoryLabelListVo> list =  Convert.convertList(objects,CategoryLabelListVo.class);
        if (null == list || list.size() == 0){
            list = baseMapper.findCategoryAndLabel();
            if (null != list && list.size() != 0){
                redisUtil.lSet(key,list);
            }
        }
        return JsonData.success(list);
    }


    /**
    * 因为修改时间需要当前时间
    * 重写mybatis-plus方法
    */
    @Override
    public ResponseData<String> updateCategoryById(Category entity) {
        entity.setUpdateDate(new Date());
        super.updateById(entity);
        //修改完毕刷新redis
        refshRedis(entity);
        return JsonData.success(entity.getId());
    }

    @Override
    public ResponseData<String> removeCategoryById(String id) {
        Category category = baseMapper.selectById(id);
        super.removeById(id);
        //修改完毕刷新redis
        refshRedis(category);
        return JsonData.success(id);
    }

    @Override
    public ResponseData<String> saveCategory(Category category) {
        super.save(category);
        //修改完毕刷新redis
        refshRedis(category);
        return JsonData.success(category.getId());
    }

    @Override
    public ResponseData<CategoryVo> getCategoryVoById(String id) {

        String key = CategoryRedisFinal.CATEGORY_KEY+id;
        //先从redis取值 如果没有值 查询数据库 讲查询得值存入redis
        Category category = (Category) redisUtil.get(key);
        if (null == category){
            category = baseMapper.selectById(id);
            if (null != category){
                redisUtil.set(key,category);
            }
        }
        CategoryVo categoryVo = new CategoryVo();
        Convert.convertType(category,categoryVo);
        return JsonData.success(categoryVo);
    }

    private  void refshRedis(Category category){

        //修改完毕刷新redis
        //分类及标签
        redisUtil.del(CategoryRedisFinal.FIND_CATEGORY_AND_LABE_LKEY);
        //正常状态分类
        redisUtil.del(CategoryRedisFinal.FIND_ALL_NORMAL_KEY);
        //当前分类
        redisUtil.del(CategoryRedisFinal.CATEGORY_KEY+category.getId());
        //指定分类下的标签集合
        redisUtil.del(CategoryRedisFinal.CATEGOR_LABELS_KEY+category.getId());
    }

}
